import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:tihai/entity/base.dart';
import 'package:tihai/entity/result.dart';

class API {
  // static const String baseURL = "http://westudy.life:12345/tihai";
  static const String baseURL = "http://172.17.8.148:12345/tihai";

  static const String createProblem = "/problem/createProblem";
  static const String selectProblemList = "/problem/selectProblemList";
  static const String deleteProblemById = "/problem/deleteProblemById";
  static const String freshProblem = "/problem/freshProblem";
  static const String randomChoice = "/problem/randomChoice";

  static const String createType = "/type/createType";
  static const String selectTypeList = "/type/selectTypeList";

  static const String loginUser = '/user/loginUser';
  static const String selectUser = '/user/selectUser';
}

BaseOptions options = BaseOptions(
    responseType: ResponseType.plain,
    baseUrl: API.baseURL,
    connectTimeout: 5000,
    receiveTimeout: 3000);

Future<List> getListAPI(
    BaseBean baseBean, String api, Map<String, dynamic> query) async {
  Response response = await Dio(options).get(api, queryParameters: query);
  var listDynamic = jsonDecode(response.data.toString());
  return (listDynamic as List<dynamic>)
      .map((e) => baseBean.fromJson(e as Map<String, dynamic>))
      .toList();
}

Future<dynamic> getAPI(
    BaseBean baseBean, String api, Map<String, dynamic> query) async {
  Response response = await Dio(options).get(api, queryParameters: query);
  Map<String, dynamic> data = jsonDecode(response.data.toString());
  return baseBean.fromJson(data);
}

Future<Result> postAPI(String api, Map<String, dynamic> data) async {
  Response response = await Dio(options).post(api, data: data);
  Map<String, dynamic> result = jsonDecode(response.data.toString());
  return Result().fromJson(result);
}
